package md.lanjing.com.module_home.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;


import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.constant.Constant;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ARouterUtils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;


import md.lanjing.com.module_home.R;
import md.lanjing.com.module_home.bean.LoginResponse;
import md.lanjing.com.module_home.contact.LoginContact;
import retrofit2.Response;

/**
 * 启动页
 */
public class SplashActivity extends MvpActivity<LoginContact.LoginPresent> implements LoginContact.ILoginView {
    @Override
    public void initData(Bundle savedInstanceState) {
        boolean isFirst= SPUtils.getBoolean(Constant.FIRST_OPEN,true,this);
        if (isFirst){
            startActivity(new Intent(this,GuideActivity.class));
            finish();
            return;
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                String phone= SPUtils.getString(Constant.PHONE,null,context);
                String loginPwd= SPUtils.getString(Constant.LOGIN_PWD,null,context);
                if (!ObjectUtils.isEmpty(phone)&&!ObjectUtils.isEmpty(loginPwd)){
                    mPresent.login(context,phone,loginPwd);
                }else {
                    startActivity(new Intent(SplashActivity.this,LoginActivity.class));
                    finish();
                }
            }
        }, 1000);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void getLoginData(Response<LoginResponse> response) {
        if (response.body().getCode()==Constant.SUCCESS_CODE){
            ARouterUtils.jump(ARouterConfig.MAIN_act);
        }else {
            startActivity(new Intent(context,LoginActivity.class));
        }
        finish();
    }

    @Override
    protected LoginContact.LoginPresent createPresent() {
        return new LoginContact.LoginPresent();
    }
}
